<?php
use \PFT\Db;

define('DAY_TIME', 3600 * 24);

//只允许管理员通过ajax方式访问
if ($_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest') {
	exit('access error');
}

include('/var/www/html/new/d/common/func.inc.php');
include('/var/www/html/new/d/module/common/Db.class.php');
$db_conf = include('/var/www/html/new/d/module/common/db.conf.php');
Db::Conf($db_conf['remote_1']);
Db::connect();

$req_past = safetxt($_REQUEST['past']);
$req_start_time = safetxt($_REQUEST['start_time']);

switch ($req_past) {
	case 'seven':
		$past = 7;$step = 1;
		$end_time = strtotime(date('Y-m-d', time()));
		break;
	case 'thirty':
		$past = 30;$step = 3;
		$end_time = strtotime(date('Y-m-d', time()));
		break;
	case 'quarter':
		$season = ceil((date('n'))/3) - 1;
	  	$start_time = mktime(0, 0, 0,$season*3-3+1,1,date('Y'));
		$end_time = mktime(24,0,0,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y'));
		$past = ($end_time - $start_time) / DAY_TIME;
		$step = 10;
		break;
	case 'month':
		$end_time = strtotime(date('Y-m-d', time()));
		$start_time = strtotime(date('Y-01-01', time()));
		$past = ($end_time - $start_time) / DAY_TIME;
		$step = 40;
		break;
	default:
		$past = 7;$step = 1;
}
$days = ceil($past / $step);

$begin_time = $end_time - $past * DAY_TIME;

$data = array();
for ($i = 0; $i < $days; $i++) {
	
	$tmp_end_time = $begin_time + DAY_TIME * $i * $step;
	if ($tmp_end_time >= time() + DAY_TIME) break;
	$tmp_begin_time = $tmp_end_time - 7 * DAY_TIME;

	$sql = "select count(distinct(aid)) as a_count,
			count(distinct(mid)) as m_count,count(distinct(tid)) as t_count
			from pft_order_statistics 
			where ddate between '" . date('Y-m-d',$tmp_begin_time) . "' and '".date('Y-m-d', $tmp_end_time) . "' 
			limit 1";
	$result = Db::fetch($sql);
	$result['date'] = date('Y-m-d', $tmp_end_time);

	if ($result) {
		$data[] = $result;
	}
}

Db::Close();

echo json_encode($data);

?>